package com.cby.library.tool.log

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.cby.library.tool.R
import com.cby.library.tool.crash.ui.listener.OnItemClickListener
import java.io.File

/**
 * @description: LogInfoAdapter
 *
 * @author ChenBaoyang
 * @date 2021/10/22 17:15
 */
class CbyLogInfoAdapter(private val context: Context, private var fileList: List<File>?) :
    RecyclerView.Adapter<CbyLogInfoAdapter.MyViewHolder>() {

    private var layoutInflater: LayoutInflater = LayoutInflater.from(context)
    private var mOnItemClickListener: OnItemClickListener? = null

    fun setOnItemClickLitener(mOnItemClickListener: OnItemClickListener?) {
        this.mOnItemClickListener = mOnItemClickListener
    }

    fun updateDatas(fileList: List<File>?) {
        this.fileList = fileList
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val inflate = layoutInflater.inflate(R.layout.item_crash_info_view, parent, false)
        return MyViewHolder(inflate)
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val file = fileList!![position]
        holder.pathTv.text = file.absolutePath

        //动态修改颜色
        val fileName = file.name.replace(".txt", "")
        holder.titleTv.text = fileName

        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener { view ->
                mOnItemClickListener!!.onItemClick(
                    view,
                    position
                )
            }
            holder.itemView.setOnLongClickListener { view ->
                mOnItemClickListener!!.onLongClick(view, position)
                false
            }
        }

    }

    override fun getItemCount() = fileList?.size ?: 0

    inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        val titleTv: TextView = itemView.findViewById(R.id.tv_title)
        val pathTv: TextView = itemView.findViewById(R.id.tv_path)

    }
}